/**
 * 路由定义
 * transitionName: 路由切换过渡动画名称
 */
import { createRouter, createWebHashHistory } from 'vue-router';
import { ElMessage } from 'element-plus';
import NProgress from 'nprogress';
import { isLogin } from '@/utils/auth';

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/launch',
      component: () => import('@/pages/index.vue'),
      children: [
        {
          path: '/launch',
          name: 'Launch',
          meta: {
            title: '导航',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('@/pages/module-launch/index.vue'),
        },
      ],
    },
    // 网格管理系统
    {
      path: '/grid-system',
      name: 'GridSystem',
      redirect: '/grid-system/screen',
      meta: {
        title: '网格管理系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-grid-system/index.vue'),
      children: [
        {
          path: '/grid-system/screen',
          name: 'GridSystemScreen',
          meta: {
            title: '网格管理系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-grid-system/grid-screen/index.vue'),
        },
        {
          path: '/grid-system/workbench',
          name: 'GridSystemWorkbench',
          meta: {
            title: '网格管理系统',
            rootName: '网格管理系统工作台',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-grid-system/grid-workbench/index.vue'),
        },
      ],
    },

    // 资源管理系统
    {
      path: '/resource-system',
      name: 'ResourceSystem',
      redirect: '/resource-system/screen',
      meta: {
        title: '资源管理系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-resource-system/index.vue'),
      children: [
        {
          path: '/resource-system/screen',
          name: 'ResourceSystemScreen',
          meta: {
            title: '资源管理系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-resource-system/resource-screen/index.vue'),
        },
        {
          path: '/resource-system/workbench',
          name: 'ResourceSystemWorkbench',
          meta: {
            title: '资源管理系统',
            rootName: '资源管理系统工作台',

            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-resource-system/resource-workbench/index.vue'),
        },
      ],
    },

    // 防火预警系统
    {
      path: '/fire-system',
      name: 'FireSystem',
      redirect: '/fire-system/screen',
      meta: {
        title: '防火预警系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-fire-system/index.vue'),
      children: [
        {
          path: '/fire-system/screen',
          name: 'FireSystemScreen',
          meta: {
            title: '防火预警系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-fire-system/fire-screen/index.vue'),
        },
        {
          path: '/fire-system/workbench',
          name: 'FireSystemWorkbench',
          meta: {
            title: '防火预警系统',
            rootName: '防火预警管理系统工作台',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-fire-system/fire-workbench/index.vue'),
        },
      ],
    },

    // 有害生物监测预警系统
    {
      path: '/pest-system',
      name: 'PestSystem',
      redirect: '/pest-system/screen',
      meta: {
        title: '有害生物监测预警系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-pest-system/index.vue'),
      children: [
        {
          path: '/pest-system/screen',
          name: 'PestSystemScreen',
          meta: {
            title: '有害生物监测预警系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-pest-system/pest-screen/index.vue'),
        },
        {
          path: '/pest-system/workbench',
          name: 'PestSystemWorkbench',
          meta: {
            title: '有害生物监测预警系统',
            rootName: '有害生物监测预警系统工作台',

            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-pest-system/pest-workbench/index.vue'),
        },
      ],
    },

    // 生物多样性系统
    {
      path: '/biology-system',
      name: 'BiologySystem',
      redirect: '/biology-system/screen',
      meta: {
        title: '生物多样性系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-biology-system/index.vue'),
      children: [
        {
          path: '/biology-system/screen',
          name: 'BiologySystemScreen',
          meta: {
            title: '生物多样性系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-biology-system/biology-screen/index.vue'),
        },
        {
          path: '/biology-system/workbench',
          name: 'BiologySystemWorkbench',
          meta: {
            title: '生物多样性系统',
            rootName: '生物多样性系统工作台',

            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-biology-system/biology-workbench/index.vue'),
        },
      ],
    },

    // 无人机管理系统
    {
      path: '/uav-system',
      name: 'UavSystem',
      redirect: '/uav-system/screen',
      meta: {
        title: '无人机管理系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-uav-system/index.vue'),
      children: [
        {
          path: '/uav-system/screen',
          name: 'UavSystemScreen',
          meta: {
            title: '无人机管理系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-uav-system/uav-screen/index.vue'),
        },
        {
          path: '/uav-system/workbench',
          name: 'UavSystemWorkbench',
          meta: {
            title: '无人机管理系统',
            rootName: '无人机管理系统工作台',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-uav-system/uav-workbench/index.vue'),
        },
      ],
    },

    // 营林项目管理系统
    {
      path: '/ylxm-system',
      name: 'ylxmSystem',
      redirect: '/ylxm-system/screen',
      meta: {
        title: '营林项目管理系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-ylxm-system/index.vue'),
      children: [
        {
          path: '/ylxm-system/screen',
          name: 'ylxmSystemScreen',
          meta: {
            title: '营林项目管理系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-ylxm-system/ylxm-screen/index.vue'),
        },
        {
          path: '/ylxm-system/workbench',
          name: 'ylxmSystemWorkbench',
          meta: {
            title: '营林项目管理系统',
            rootName: '营林项目管理系统工作台',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-ylxm-system/ylxm-workbench/index.vue'),
        },
      ],
    },

    // 种质资源管理系统
    {
      path: '/zhongZhi-system',
      name: 'ZhongZhiSystem',
      redirect: '/zhongZhi-system/screen',
      meta: {
        title: '种质资源管理系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-zhongZhi-system/index.vue'),
      children: [
        {
          path: '/zhongZhi-system/screen',
          name: 'ZhongZhiSystemScreen',
          meta: {
            title: '种质资源管理系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-zhongZhi-system/zhongZhi-screen/index.vue'),
        },
        {
          path: '/zhongZhi-system/workbench',
          name: 'ZhongZhiSystemWorkbench',
          meta: {
            title: '种质资源管理系统',
            rootName: '种质资源管理系统工作台',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-zhongZhi-system/zhongZhi-workbench/index.vue'),
        },
      ],
    },

    // 林木种苗管理系统
    {
      path: '/sprout-system',
      name: 'SproutSystem',
      redirect: '/sprout-system/screen',
      meta: {
        title: '林木种苗管理系统',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('@/pages/module-sprout-system/index.vue'),
      children: [
        {
          path: '/sprout-system/screen',
          name: 'SproutSystemScreen',
          meta: {
            title: '林木种苗管理系统',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-sprout-system/sprout-screen/index.vue'),
        },
        {
          path: '/sprout-system/workbench',
          name: 'SproutSystemWorkbench',
          meta: {
            title: '林木种苗管理系统',
            rootName: '林木种苗管理系统工作台',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-sprout-system/sprout-workbench/index.vue'),
        },
      ],
    },

    {
      path: '/login',
      name: 'Login',
      meta: {
        title: '登录',
        needLogin: false,
        transitionName: 'router',
      },
      component: () => import('../pages/module-login/index.vue'),
    },
    // 系统管理
    {
      path: '/base-system',
      name: 'BaseSystem',
      redirect: '/base-menu',
      meta: {
        title: '系统管理',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import('../pages/module-base-system/index.vue'),
      children: [
        {
          path: '/base-menu',
          name: 'baseMenu',
          meta: {
            title: '系统管理-菜单管理',
            name: '菜单管理',
            icon: 'menu',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-base-system/base-menu/index.vue'),
        },
        {
          path: '/base-role',
          name: 'baseRole',
          meta: {
            title: '系统管理-角色管理',
            name: '角色管理',
            icon: 'role',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-base-system/base-role/index.vue'),
        },
        {
          path: '/base-user',
          name: 'baseUser',
          meta: {
            title: '系统管理-用户管理',
            name: '用户管理',
            icon: 'peoples',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-base-system/base-user/index.vue'),
        },

        // {
        //   path: '/base-dictionary',
        //   name: 'baseDictionary',
        //   meta: {
        //     title: '系统管理-字典管理',
        //     name: '字典管理',
        //     icon: 'dict',
        //     needLogin: true,
        //     transitionName: 'router',
        //   },
        //   component: () => import('../pages/module-base-system/base-dictionary/index.vue'),
        // },
        {
          path: '/base-organization',
          name: 'baseOrganization',
          meta: {
            title: '系统管理-组织管理',
            name: '组织管理',
            icon: 'tree',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-base-system/base-organization/index.vue'),
        },
        {
          path: '/base-log',
          name: 'baseLog',
          meta: {
            title: '系统管理-日志管理',
            name: '日志管理',
            icon: 'document',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-base-system/base-log/index.vue'),
        },
        {
          path: '/base-layer-data',
          name: 'baseLayerData',
          meta: {
            title: '系统管理-数据管理',
            name: '数据管理',
            icon: 'dict',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('../pages/module-base-system/base-layer-data/index.vue'),
        },
      ],
    },
    {
      path: '/:pathMatch(.*)',
      name: '404',
      meta: {
        title: '404',
        needLogin: false,
        transitionName: 'router',
      },
      component: () => import('@/components/Router404.vue'),
    },
  ],
});

router.beforeEach((to, from, next) => {
  NProgress.start();

  // 如果需要登录
  if (to.meta.needLogin) {
    // 如果有token 则直接放行
    if (isLogin()) {
      next();
    } else {
      // 否则去登录页
      ElMessage.error('请先登录！');
      next('/login');
    }
  } else {
    // 不需要登录则直接放行
    next();
  }
});

// 修改标题的工作可以放在全局后置守卫
router.afterEach((to) => {
  setTimeout(() => {
    NProgress.done();
  }, 700);

  if (to.meta.title) {
    document.title = `${import.meta.env.VITE_APP_NAME} - ${to.meta.title}`;
  }
});

export default router;
